//
//  DatabaseAccessor.m
//  UGuideUS
//
//  Created by Will Wang on 22/07/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DatabaseAccessor.h"

@implementation DatabaseAccessor

@synthesize theLocation=_theLocation;
@synthesize userName=_userName;
@synthesize commentToBeInserted=_commentToBeInserted;

-(NSString*) searchComments{
    
    NSURL *url = [NSURL URLWithString:@"http://uguideus.comyr.com/finaldatabase.php"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData                                                           timeoutInterval:60];
    [req setHTTPMethod:@"POST"];                
    [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    NSString *postData = [NSString stringWithFormat:@"mode=%@&location=%@", @"view", _theLocation];
    
    NSString *length = [NSString stringWithFormat:@"%d", [postData length]];        
    [req setValue:length forHTTPHeaderField:@"Content-Length"];        
    [req setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
    
    NSHTTPURLResponse* urlResponse = nil;
    NSError *error;  
    NSData *responseData = [NSURLConnection sendSynchronousRequest:req
                                                 returningResponse:&urlResponse 
                                                             error:&error]; 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
    //NSLog(@"%@",responseData);
    NSLog(@"%@",responseString);
    return responseString;
}

-(NSString*) insertCommentWithUserName{
    NSLog(@"I am in insertComment functioin");
    NSURL *url = [NSURL URLWithString:@"http://uguideus.comyr.com/finaldatabase.php"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData                                                           timeoutInterval:60];
    [req setHTTPMethod:@"POST"];                
    [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    NSString *postData = [NSString stringWithFormat:@"mode=%@&location=%@&username=%@&comment=%@", @"insert",  _theLocation,_userName,_commentToBeInserted];
    
    NSLog(@"mode=%@&location=%@&username=%@&comment=%@", @"insert",  _theLocation,_userName,_commentToBeInserted);
    
    NSString *length = [NSString stringWithFormat:@"%d", [postData length]];        
    [req setValue:length forHTTPHeaderField:@"Content-Length"];        
    [req setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
    NSHTTPURLResponse* urlResponse = nil;
    NSError *error;  
    NSData *responseData = [NSURLConnection sendSynchronousRequest:req
                                                 returningResponse:&urlResponse 
                                                             error:&error]; 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
    //NSLog(@"%@",responseData);
    NSLog(@"%@",responseString);
    return responseString;
}

@end
